import axios, {
    AxiosRequestConfig,
    AxiosResponse,
    InternalAxiosRequestConfig,
    AxiosError,
} from 'axios'

const baseURL = 'http://188.102.16.117:8002';

const service = axios.create({
    baseURL,
    timeout: 120000,
})

// 请求拦截器
service.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        // 模拟 Token 设置，可按需调整
        const token = localStorage.getItem('token')
        config.headers.authKey = 'szyg_20250710'


        // GET 请求携带 data，转为 params
        if (config.method?.toUpperCase() === 'GET' && config.data) {
            config.params = config.data
            delete config.data
        }

        return config
    },
    (error: AxiosError) => {
        return Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    (response: AxiosResponse) => {
        return response.data
    },
    (error) => {
        // 可在此添加全局错误提示
        return Promise.reject(error)
    }
)

// 泛型支持，返回数据类型可自动推导
export default function request<T = any>(
    config: AxiosRequestConfig
): Promise<T> {
    return service(config)
}
